package com.inspur.jxpcxt.fragments;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.inspur.jxpcxt.LoginActivity;
import com.inspur.jxpcxt.service.RetrieveService;
import com.inspur.jxpcxt.widget.ShowPswdDialog;
import com.inspur.jypcxt.R;

public class RetriveFragment extends Fragment {
	private static final int WAIT_TIME = 1000 * 10;
	private EditText mSnoEt = null;
	private EditText mSchoolEt = null;
	private Button mRetrivePswdBtn = null;

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
		{
			// TODO Auto-generated method stub
			View mRootView = inflater.inflate(R.layout.fragment_retrievepswd,
					container, false);
			initUI(mRootView);
			return mRootView;
		}

	private void initUI(View mRootView)
		{
			// TODO Auto-generated method stub
			mSnoEt = (EditText) mRootView.findViewById(R.id.et_sno);
			mSchoolEt = (EditText) mRootView.findViewById(R.id.et_school);
			mRetrivePswdBtn = (Button) mRootView
					.findViewById(R.id.btn_retrieve_pswd);

			mRetrivePswdBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v)
					{

						new AsyncTask<Void, Void, String>() {

							@Override
							protected String doInBackground(Void... params)
								{
									// 访问服务器，用dialog方式将密码显示是出来
									String result = RetrieveService
											.RetriveByClientGet(mSnoEt
													.getText().toString()
													.trim(), mSchoolEt
													.getText().toString()
													.trim());
									return result;
								}

							protected void onPostExecute(String result)
								{
									if (result == null || result.isEmpty()) {
										return;
									}
									// 判断
									if (result.equals("失败")) {
										Toast.makeText(getContext(),
												"请求失败,请核实账号及密保",
												Toast.LENGTH_SHORT).show();
									} else {
										// dialog显示
										ShowPswdDialog.showPswd(getContext(),
												result);
										// 10之后返回登录界面
										final Intent intent = new Intent(
												getContext(),
												LoginActivity.class);
										// 计时器中的时间表方法
										new Timer().schedule(new TimerTask() {

											@Override
											public void run()
												{
													startActivity(intent);
													getActivity().finish();
												}
										}, WAIT_TIME);
									}
								};
						}.execute();

					}
			});
		}
//	public boolean onKey(View view, int i, KeyEvent keyEvent) {
//        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
//            if (i == KeyEvent.KEYCODE_BACK) {  //表示按返回键 时的操作
//                    return false;
//                } //后退
//        }
//        return false;
//    }
}

